package com.kxmall.web.controller.decorate.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.kxmall.common.core.domain.PageQuery;
import com.kxmall.common.core.page.TableDataInfo;
import com.kxmall.common.utils.StringUtils;
import com.kxmall.decorate.domain.KxPageTemplate;
import com.kxmall.decorate.domain.bo.KxPageTemplateBo;
import com.kxmall.decorate.domain.vo.KxPageTemplateVo;
import com.kxmall.decorate.mapper.KxPageTemplateMapper;
import com.kxmall.web.controller.decorate.service.IKxPageTemplateService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Collection;
import java.util.List;
import java.util.Map;

/**
 * 首页页面模板Service业务层处理
 *
 * @author kxmall
 * @date 2023-11-05
 */
@RequiredArgsConstructor
@Service
public class KxPageTemplateServiceImpl implements IKxPageTemplateService {

    private final KxPageTemplateMapper baseMapper;

    /**
     * 查询首页页面模板
     */
    @Override
    public KxPageTemplateVo queryById(Long id) {
        return baseMapper.selectVoById(id);
    }

    /**
     * 查询首页页面模板列表
     */
    @Override
    public TableDataInfo<KxPageTemplateVo> queryPageList(KxPageTemplateBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<KxPageTemplate> lqw = buildQueryWrapper(bo);
        Page<KxPageTemplateVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询首页页面模板列表
     */
    @Override
    public List<KxPageTemplateVo> queryList(KxPageTemplateBo bo) {
        LambdaQueryWrapper<KxPageTemplate> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<KxPageTemplate> buildQueryWrapper(KxPageTemplateBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<KxPageTemplate> lqw = Wrappers.lambdaQuery();
        lqw.eq(bo.getStatus() != null, KxPageTemplate::getStatus, bo.getStatus());
        lqw.eq(StringUtils.isNotBlank(bo.getContent()), KxPageTemplate::getContent, bo.getContent());
        return lqw;
    }

    /**
     * 新增首页页面模板
     */
    @Override
    public Boolean insertByBo(KxPageTemplateBo bo) {
        KxPageTemplate add = BeanUtil.toBean(bo, KxPageTemplate.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改首页页面模板
     */
    @Override
    public Boolean updateByBo(KxPageTemplateBo bo) {
        KxPageTemplate update = BeanUtil.toBean(bo, KxPageTemplate.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(KxPageTemplate entity) {
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除首页页面模板
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if (isValid) {
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }

    /**
     * 启用一个其他的直接设置未启用
     *
     * @param bo
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean homeTemplateLaunch(KxPageTemplateBo bo) {

        KxPageTemplate update = new KxPageTemplate();
        update.setStatus(0);
        baseMapper.update(update, new LambdaQueryWrapper<KxPageTemplate>().eq(KxPageTemplate::getStatus, 1));

        update.setId(bo.getId());
        update.setStatus(1);
        return baseMapper.updateById(update) > 0;

    }
}
